


//
//  XJAFNCliect.m
//  XJMCustomer
//
//  Created by Mr.hu on 2017/10/9.
//  Copyright © 2017年 Mr.hu. All rights reserved.
//

#import "XJAFNCliect.h"

@implementation XJAFNCliect
//单例初始化
+ (XJAFNCliect *)sharedClient
{
    static XJAFNCliect *_naClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        [config setHTTPAdditionalHeaders:@{ @"User-Agent" : @"BlueMobi iOS team"}];
        
        //设置我们的缓存大小 其中内存缓存大小设置10M  磁盘缓存5M
        NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:10 * 1024 * 1024
                                                          diskCapacity:50 * 1024 * 1024
                                                              diskPath:nil];
        
        [config setURLCache:cache];
        
        _naClient = [[XJAFNCliect alloc] initWithBaseURL:nil
                                    sessionConfiguration:config];
        [_naClient.requestSerializer setTimeoutInterval:8];
        _naClient.responseSerializer = [AFHTTPResponseSerializer serializer];
        
    });
    
    return _naClient;
}

//post请求
- (NSURLSessionDataTask *)postRequest:(NSString *)url parameters:(id)parameters completion:( void (^)(id result, NSError *error) )completion
{
    NSURLSessionDataTask *task = [self POST:url
                                 parameters:parameters
                                    success:^(NSURLSessionDataTask *task, id responseObject) {
                                        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
                                        if (httpResponse.statusCode == 200) {
                                            dispatch_async(dispatch_get_main_queue(), ^{
                                                completion(responseObject, nil);
                                            });
                                        } else {
                                            dispatch_async(dispatch_get_main_queue(), ^{
                                                completion(nil, nil);
                                            });
                                            NSLog(@"Received: %@", responseObject);
                                            NSLog(@"Received HTTP %ld", (long)httpResponse.statusCode);
                                        }
                                    } failure:^(NSURLSessionDataTask *task, NSError *error) {
                                        dispatch_async(dispatch_get_main_queue(), ^{
                                            completion(nil, error);
                                        });
                                    }];
    return task;
}

//get请求
- (NSURLSessionDataTask *)getRequest:(NSString *)url parameters:(id)parameters completion:( void (^)(id result, NSError *error) )completion
{
    NSURLSessionDataTask *task = [self GET:url
                                parameters:parameters
                                   success:^(NSURLSessionDataTask *task, id responseObject) {
                                       NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
                                       if (httpResponse.statusCode == 200) {
                                           dispatch_async(dispatch_get_main_queue(), ^{
                                               completion(responseObject, nil);
                                           });
                                       } else {
                                           dispatch_async(dispatch_get_main_queue(), ^{
                                               completion(nil, nil);
                                           });
                                           NSLog(@"Received: %@", responseObject);
                                           NSLog(@"Received HTTP %ld", (long)httpResponse.statusCode);
                                       }
                                   } failure:^(NSURLSessionDataTask *task, NSError *error) {
                                       dispatch_async(dispatch_get_main_queue(), ^{
                                           completion(nil, error);
                                       });
                                   }];
    return task;
}
//put请求
- (NSURLSessionDataTask *)putRequest:(NSString *)url parameters:(id)parameters completion:( void (^)(id result, NSError *error) )completion{
    NSURLSessionDataTask *task = [self PUT:url
                                parameters:parameters
                                   success:^(NSURLSessionDataTask *task, id responseObject) {
                                       NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
                                       if (httpResponse.statusCode == 200) {
                                           dispatch_async(dispatch_get_main_queue(), ^{
                                               completion(responseObject, nil);
                                           });
                                       } else {
                                           dispatch_async(dispatch_get_main_queue(), ^{
                                               completion(nil, nil);
                                           });
                                           NSLog(@"Received: %@", responseObject);
                                           NSLog(@"Received HTTP %ld", (long)httpResponse.statusCode);
                                       }
                                   } failure:^(NSURLSessionDataTask *task, NSError *error) {
                                       dispatch_async(dispatch_get_main_queue(), ^{
                                           completion(nil, error);
                                       });
                                   }];
    return task;
}
//delete请求
- (NSURLSessionDataTask *)deleteRequest:(NSString *)url parameters:(id)parameters completion:( void (^)(id result, NSError *error) )completion{
    NSURLSessionDataTask *task = [self DELETE:url
                                   parameters:parameters
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
                                          if (httpResponse.statusCode == 200) {
                                              dispatch_async(dispatch_get_main_queue(), ^{
                                                  completion(responseObject, nil);
                                              });
                                          } else {
                                              dispatch_async(dispatch_get_main_queue(), ^{
                                                  completion(nil, nil);
                                              });
                                              NSLog(@"Received: %@", responseObject);
                                              NSLog(@"Received HTTP %ld", (long)httpResponse.statusCode);
                                          }
                                      } failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          dispatch_async(dispatch_get_main_queue(), ^{
                                              completion(nil, error);
                                          });
                                      }];
    return task;
    
}
//上传data
- (NSURLSessionDataTask *)dataRequest:(NSString *)url parameters:(id)parameters data:(NSData *)data completion:( void (^)(id result, NSError *error) )completion
{
    NSURLSessionDataTask *task = [self POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (data) {
            [formData appendPartWithFileData:data
                                        name:@"head"
                                    fileName:@"image.jpg"
                                    mimeType:@"image/jpeg"];
        }
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
        if (httpResponse.statusCode == 200) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(responseObject, nil);
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(nil, nil);
            });
            NSLog(@"Received: %@", responseObject);
            NSLog(@"Received HTTP %@", @(httpResponse.statusCode));
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(nil, error);
        });
    }];
    return task;
}
//上传data
- (NSURLSessionDataTask *)dataRequest:(NSString *)url parameters:(id)parameters data:(NSData *)data dataName:(NSString *)dataName completion:( void (^)(id result, NSError *error) )completion{
    
    //    NSURLSessionDataTask *tesk=[self put:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    //
    //    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    //
    //    }];
    
    NSURLSessionDataTask *task = [self POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (data) {
            [formData appendPartWithFileData:data
                                        name:dataName
                                    fileName:@"image.jpg"
                                    mimeType:@"image/jpeg"];
        }
        
        if ([dataName isEqualToString:@"voice"]) {
            [formData appendPartWithFileURL:[NSURL fileURLWithPath:parameters[@"voicePath"]] name:@"voice" error:nil];
        }
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
        if (httpResponse.statusCode == 200) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(responseObject, nil);
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(nil, nil);
            });
            NSLog(@"Received: %@", responseObject);
            NSLog(@"Received HTTP %@", @(httpResponse.statusCode));
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(nil, error);
        });
    }];
    return task;
}

#warning mark -- datas 以及 dataNames 必须个数一致
- (NSURLSessionDataTask *)dataRequest:(NSString *)url parameters:(id)parameters datas:(NSArray *)datas dataNames:(NSArray *)dataNames completion:( void (^)(id result, NSError *error) )completion{
    NSURLSessionDataTask *task = [self POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (datas.count > 0) {
            
            for (int i = 0; i<datas.count; i++) {
                [formData appendPartWithFileData:datas[i]
                                            name:dataNames[i]
                                        fileName:@"image.jpg"
                                        mimeType:@"image/jpeg"];
            }
        }
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
        if (httpResponse.statusCode == 200) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(responseObject, nil);
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(nil, nil);
            });
            NSLog(@"Received: %@", responseObject);
            NSLog(@"Received HTTP %ld", (long)httpResponse.statusCode);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(nil, error);
        });
    }];
    return task;
}
//上传多张图片以及语音
- (NSURLSessionDataTask *)imgVoiceRequest:(NSString *)url parameters:(id)parameters datas:(NSArray *)datas Voice:(NSString *)fileUrl completion:( void (^)(id result, NSError *error) )completion
{
    NSURLSessionDataTask *task = [self POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (datas.count > 0) {
            for (int i = 0; i<datas.count; i++) {
                NSData *data = UIImageJPEGRepresentation(datas[i],0.1);
                [formData appendPartWithFileData:data
                                            name:@"file"
                                        fileName:@"image.jpg"
                                        mimeType:@"image/jpeg"];
            }
        }
        if (fileUrl) {
            [formData appendPartWithFileURL:[NSURL fileURLWithPath:fileUrl] name:@"voice" error:nil];
        }
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
        if (httpResponse.statusCode == 200) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(responseObject, nil);
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(nil, nil);
            });
            NSLog(@"Received: %@", responseObject);
            NSLog(@"Received HTTP %ld", (long)httpResponse.statusCode);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            completion(nil, error);
        });
    }];
    return task;
    
}


@end
